package com.honey.solr.daosupport.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.compilation.java.TryCatchBlock;
import com.honey.core.types.ExceptionFullyQualifiedJavaType;
import com.honey.generator.java.AbstractClassName;
import com.honey.solr.SolrConstant;
import com.honey.solr.SolrPlugin;

public class Query4Method extends AbstractClassName {
	
	private SolrPlugin solrPlugin = null;
	
	public Query4Method(SolrPlugin solrPlugin ){
		this.solrPlugin = solrPlugin;
	}
	public Method createMethod(){
		Method answer =null;
		if(solrPlugin.getQueryForPage() != null ){		
			answer = new Method("query");
			answer.setReturnType(SolrConstant.QUERY_RESPONSE);
			answer.addException(SolrConstant.SOLR_SERVER_EXCEPTION);
			
			Parameter parameter = new Parameter(SolrConstant.SOLR_QUERY,"query");
			answer.addParameter(parameter) ;
			parameter = new Parameter(solrPlugin.getQueryForPage(),"page");
			answer.addParameter(parameter) ;

			answer.addBodyLine("QueryResponse answer = null ");
			IfBlock  block = new IfBlock("page == null") ;
			block.addBodyLine("page = new Page()");
			answer.addBlocks(block);
			
//			answer.addBodyLine("query.setHighlight(this.highlight)");
//			answer.addBodyLine("query.setHighlightSimplePre(this.highlightSimplePre)");
//			answer.addBodyLine("query.setHighlightSimplePost(this.highlightSimplePost)");
//			
//			answer.addBodyLine("query.setStart(page.getBegin())");
//			answer.addBodyLine("query.setRows(page.getInterval())");
//			answer.addBodyLine("if( page.getOrderBy() != null ){");
//			answer.addBodyLine("query.addSortField( page.getSort(), page.getDir() ==  Page.Dir.ASC ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc )");
//			answer.addBodyLine("}");
			
			answer.addBodyLine("addQueryParameter( query, page )");
			
			
			answer.addBodyLine("answer = "+SolrConstant.SOLR_SERVER_FIELD_NAME+".query(query)") ;
			answer.addBodyLine("page.setTotal((int)answer.getResults().getNumFound())");
			
			answer.addBodyLine("return answer") ;
		}
		return answer;
	}
}
